<template>
  <div>
    <!-- data: 表格对应的数据 -->
    <!-- row-key：选择的依据，通过id选择 -->
    <!-- default-expand-all：默认展开所有子集 -->
    <!-- tree-props：属性数据的属性 -->
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="rolename" label="角色名称" width="120"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="edit(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { roleList } from "../../api";
export default {
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.getMenu();
  },
  methods: {
    async getMenu() {
      let res = await roleList();
      this.tableData = res.list;
    },
    // 编辑
    edit(info) {
      // 先子传父，后父传子  list->index->info
      this.$emit('edit', info);
    },
    // 删除
    del(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 真的删除
        .then(async () => {
          let res = await menuDel(id);
          if(res.code === 200) {
            // 用接口返回的最新数据更新页面
            this.tableData = res.list;
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        })
        // 取消删除
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>